﻿/*****************************************************************
                  jQuery Validate扩展验证方法  
                  by yishuaijun
                  2015-7-2
*****************************************************************/
$(function () {
    $("form").each(function () {
        $(this).validate({
            showErrors: function (errorMap, errorList) {
                var msg = "";
                $.each(errorList, function (i, v) {
                    msg += v.message + "<br/>";
                });
                if (msg != "") {
                    layer.msg(msg, { time: 3000, icon: 0 });
                }
                if (msg == "") {
                    layer.closeAll();
                }
                return false;
            }
        });
    });

    jQuery.validator.addMethod('atLeastOneChecked', function (value, element) {
        var checkedCount = 0;
        $(element).each(function () {
            if ($(this).attr('checked')) { checkedCount++; }
        });
        return checkedCount > 0;

    }, "请选择至少一项");
    jQuery.validator.addMethod('isExsitErrorUser', function (value, element) {
        return false;
    }, "有不存在的用户");
    jQuery.validator.addMethod('errorMsg', function (value, element) {
        return false;
    }, "输入的信息有误");
    // 手机号码验证    
    jQuery.validator.addMethod("isTelphone", function (value, element) {
        return this.optional(element) || isTelphone(value);
    }, "请输入有效的手机号码");

    jQuery.validator.addMethod("isEmail", function (value, element) {
        return this.optional(element) || isEmail(value);
    }, "请输入有效的邮箱");

    // 验证非负整数    
    jQuery.validator.addMethod("isNonNegInt", function (value, element) {
        var num = /^[0-9]*[0-9][0-9]*$/;
        return this.optional(element) || (num.test(value));
    }, "请正确填写数值");

    // 联系电话(手机/电话皆可)验证   
    jQuery.validator.addMethod("isTel", function (value, element) {
        var length = value.length;
        var mobile = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
        var tel = /^(\d{3,4}-?)?\d{7,9}$/g;
        return this.optional(element) || tel.test(value) || (length == 11 && mobile.test(value));
    }, "请输入有效的电话号码！");
    //验证整型

    // 匹配qq      
    jQuery.validator.addMethod("isQq", function (value, element) {
        return this.optional(element) || /^[1-9]\d{4,12}$/;
    }, "匹配QQ");

    // 邮政编码验证    
    jQuery.validator.addMethod("isZipCode", function (value, element) {
        var zip = /^[0-9]{6}$/;
        return this.optional(element) || (zip.test(value));
    }, "请正确填写您的邮政编码");

    // 身份证号码验证
    jQuery.validator.addMethod("isIdCardNo", function (value, element) {
        return this.optional(element) || isIdCardNo(value);
    }, "请填写正确的身份证号码");

    jQuery.validator.addMethod("isPassport", function (value, element, type) {
        return this.optional(element) || isPassport(value);
    }, "护照格式不正确");
    jQuery.validator.addMethod("isHKMacao", function (value, element, type) {
        return this.optional(element) || (isHKMacao(value));
    }, "港澳通行证格式不正确");

    jQuery.validator.addMethod("isTaiwan", function (value, element, type) {
        if ($(type).val() == "4") {
            var re1 = /^[0-9]{8}$/;
            var re2 = /^[0-9]{10}$/;
            return this.optional(element) || (re1.test(value)) || (re2.test(value))
        } else {
            return true;
        }
    }, "台湾通行证格式不正确");
    // IP地址验证   
    jQuery.validator.addMethod("ip", function (value, element) {
        return this.optional(element) || /^(([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5])))\.)(([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5])))\.){2}([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5])))$/.test(value);
    }, "请填写正确的IP地址");

    // 字符验证，只能包含中文、英文、数字、下划线等字符。    
    jQuery.validator.addMethod("stringCheck", function (value, element) {
        return this.optional(element) || /^[a-zA-Z0-9\u4e00-\u9fa5-_]+$/.test(value);
    }, "只能包含中文、英文、数字、下划线等字符");

    // 匹配english  
    jQuery.validator.addMethod("isEnglish", function (value, element) {
        return this.optional(element) || /^[A-Za-z]+$/.test(value);
    }, "匹配english");

    // 匹配汉字  
    jQuery.validator.addMethod("isChinese", function (value, element) {
        return this.optional(element) || /^[\u4e00-\u9fa5]+$/.test(value);
    }, "匹配汉字");

    // 匹配中文(包括汉字和字符) 
    jQuery.validator.addMethod("isChineseChar", function (value, element) {
        return this.optional(element) || /^[\u0391-\uFFE5]+$/.test(value);
    }, "匹配中文(包括汉字和字符) ");

    // 判断是否为合法字符(a-zA-Z0-9-_)
    jQuery.validator.addMethod("isRightfulString", function (value, element) {
        return this.optional(element) || /^[A-Za-z0-9_-]+$/.test(value);
    }, "判断是否为合法字符(a-zA-Z0-9-_)");

    // 判断是否为合法字符(a-zA-Z0-9)
    jQuery.validator.addMethod("isRightfulStringNounderline", function (value, element) {
        return this.optional(element) || /^[A-Za-z0-9]+$/.test(value);
    }, "只能包含英文、数字等字符");

    // 判断是否包含中英文特殊字符，除英文"-_"字符外
    jQuery.validator.addMethod("CanNotContainsSpecialChar", function (value, element) {
        var reg = RegExp(/[(\ )(\`)(\~)(\!)(\@)(\#)(\$)(\%)(\^)(\&)(\*)(\()(\))(\+)(\=)(\|)(\{)(\})(\')(\:)(\;)(\')(',)(\[)(\])(\.)(\<)(\>)(\/)(\?)(\~)(\！)(\@)(\#)(\￥)(\%)(\…)(\&)(\*)(\（)(\）)(\—)(\+)(\|)(\{)(\})(\【)(\】)(\‘)(\；)(\:)(\”)(\“)(\’)(\。)(\，)(\、)(\？)]+/);
        return this.optional(element) || !reg.test(value);
    }, "不能含有中英文特殊字符");
});
//身份证号码的验证规则
function isIdCardNo(code) {
    var city = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江 ", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北 ", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏 ", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外 " };
    var tip = "";
    var pass = true;

    if (!code || !/^([1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/i.test(code)) {
        pass = false;
    }
    else if (!city[code.substr(0, 2)]) {
        pass = false;
    }
    else {
        //18位身份证需要验证最后一位校验位
        if (code.length == 18) {
            if (!verifyCode(code)) {
                //alert("身份证最后校验错误！");
            }
            return verifyCode(code);
        }
    }
    return pass;
}
function verifyCode(id) {
    if (id.length != 18)
        return false;
    /*1、从第一位到第十七位的系数分别为：
      7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2 
      将这17位数字和系数相乘的结果相加。 */
    var arr = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
    var sum = 0;
    for (var i = 0; i < arr.length; i++) {
        sum += parseInt(id.charAt(i)) * arr[i];
    }
    //2、用加出来和除以11，看余数，
    var c = sum % 11;
    //3、分别对应的最后一位身份证的号码为：1－0－X－9－8－7－6－5－4－3－2
    var ch = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
    var code = ch[c];
    var last = id.charAt(17);
    last = last == 'x' ? 'X' : last;
    return last == code;
}

function isTelphone(value) {
    var mobile = /^((1[358][0-9])|(14[57])|(17[0678])|(19[89])|(166]))\d{8}$/;
    return mobile.test(value);
}
function isEmail(value) {
    var email = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/g;
    return email.test(value);
}
function isPassport(value) {
    var re1 = /^[a-zA-Z]{5,17}$/;
    var re2 = /^[a-zA-Z0-9]{5,17}$/;
    return (re2.test(value)) || re1.test(value);
}
function isHKMacao(value) {
    var re = /^[HMhm]{1}([0-9]{10}|[0-9]{8})$/;
    return (re.test(value));
}
function isTaiwan(value) {
    var re1 = /^[0-9]{8}$/;
    var re2 = /^[0-9]{10}$/;
    return (re2.test(value)) || re1.test(value);
}